/* Copyright (C) 2008 Lorenzo Braghetto
 * 
 * This file is part of BatteryDiff Widget.
 * 
 *   BatteryDiff Widget is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *   
 *  BatteryDiff Widget is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
*/

package com.mono.diffwidget;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.RadioGroup;

public class Page extends Activity {
	public int time = 0;
	public static final String PREFS_NAME = "DiffWidgetPrefs";
	public static final String PREFS_NAME2 = "TimePref";
	int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        Intent launchIntent = getIntent();
        Bundle extras = launchIntent.getExtras();
        mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);

        // set the result for cancel first
        // if the user cancels, then the appWidget
        // should not appear
        Intent cancelResultValue = new Intent();
        cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
        		mAppWidgetId);
        setResult(RESULT_CANCELED, cancelResultValue);		

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		setContentView(R.layout.page);

		final RadioGroup radiogroup1 = (RadioGroup) findViewById(R.id.group1);
		View aboutButton = findViewById(R.id.About);
		final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
		final CheckBox batt = (CheckBox) findViewById(R.id.batt);
		SharedPreferences settingz = getSharedPreferences(PREFS_NAME2, 0);
		time = settingz.getInt("time", 0);

		aboutButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {

				Dialog dialog = new Dialog(Page.this);
				dialog.setContentView(R.layout.aboutdialog);
				dialog.setTitle("About");
				dialog.show();
			}
		});
		View okButton = findViewById(R.id.Button01);
		okButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				int radioCheckedId = radiogroup1.getCheckedRadioButtonId();
				if (radioCheckedId == R.id.Radio01) {
					time = 1;
				}
				if (radioCheckedId == R.id.Radio02) {
					time = 2;
				}
				if (radioCheckedId == R.id.Radio03) {
					time = 3;
				}
				boolean checked = checkbox.isChecked();
				boolean checkedbatt = batt.isChecked();
				SharedPreferences settings2 = getSharedPreferences(PREFS_NAME2,
						0);
				SharedPreferences.Editor editor2 = settings2.edit();
				editor2.putBoolean("notification", checked);
				editor2.putBoolean("batt", checkedbatt);
				editor2.putInt("time", time);
				editor2.putBoolean("config", true);
				editor2.commit();
				SharedPreferences settings = getSharedPreferences(PREFS_NAME,
						0);
				SharedPreferences.Editor editor = settings.edit();
				editor.clear();
				editor.commit();
				
				Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                                mAppWidgetId);
                setResult(RESULT_OK, resultValue);
				
				finish();
			}
		});

	}
	
	public static void cancelAlarm(Context context) {

		AlarmManager alarmManager = (AlarmManager) context
				.getSystemService(ALARM_SERVICE);

		Intent scheduleIntent = new Intent(context, BatteryDiffService.class);
		PendingIntent pi = PendingIntent.getService(context, 0,
				scheduleIntent, PendingIntent.FLAG_CANCEL_CURRENT);

		alarmManager.cancel(pi);

	}


}
